package com.xzx.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/7/5 22:56
 */
public class HDFSClient3 {
    private static final Logger logger = LoggerFactory.getLogger(HDFSClient3.class);

    public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {

    }

    @Test
    public void testCreate() throws URISyntaxException, IOException {
        Configuration config = new Configuration();
        //1.获取hdfs客户端对象
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://localhost:9000"), config);

        //2、执行命令
        fileSystem.create(new Path("/test"));
        //3.关闭资源
        fileSystem.close();
        logger.info("========over===");
    }

    @Test
    public void testWrite() throws URISyntaxException, IOException {
        Configuration config = new Configuration();
        //1.获取hdfs客户端对象
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://localhost:9000"), config);
        fileSystem.delete(new Path("/test"), true);

        //2、执行命令
        FSDataOutputStream outputStream = fileSystem.create(new Path("/test"));
        outputStream.write("Hello World".getBytes());
        outputStream.flush();
        //3.关闭资源
        fileSystem.close();
        logger.info("========over===");
    }

    @Test
    public void testDelete() throws URISyntaxException, IOException {
        Configuration config = new Configuration();
        //1.获取hdfs客户端对象
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://localhost:9000"), config);

        //2、执行命令
        fileSystem.delete(new Path("/test"), true);
        //3.关闭资源
        fileSystem.close();
        logger.info("========over===");
    }
}
